package com.work;
import com.dao.BaseDAO;
import com.model.User;
import com.service.impl.UserManagerImpl;
import com.util.HibernateUtil;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by zhanghf on 2015/6/25.
 */
public class UserEntityTest {
    /**
     * Loger4j的logger
     */
    private static final Logger logger = Logger.getLogger(UserEntityTest.class);
//    public static void main(String[] args){
//        //getUser();
//    }
    //public static void main(String[] arg){
    public void testHibernateUtil(){
        UserManagerImpl umi = new UserManagerImpl();
        //BaseDAO userDao = new BaseDAO();
        //add
        User user1 = new User();
        user1.setUsername("asdf");
        user1.setPassword("1234");
        //user1.setId(3);
        user1.setNickname("88888888888");
        if (umi.createUser(user1) != 0L){
            System.out.println("添加了一条数据：asdf-->1234");
        }

        //get
        User user2 = umi.getUserById(2L);
        if (user2 != null) {
            System.out.println("用户ID为2的用户数据：" + user2.getUsername() + "-->" + user2.getPassword());
        }
        //update
        user2.setUsername("test");
        user2.setPassword("789");
        if (umi.modifyUser(user2)){
            System.out.println("用户ID为2的用户数据："+user2.getUsername()+"-->"+user2.getPassword());
        }

        //query all
        String[] para ={"asdf"};
        //List<User> list = userDao.query("from User u where u.username=?", para);
        Map<String,String> mapUser = new HashMap<String, String>();
        mapUser.put("usrname","asdf");
        List<User> list = umi.getUser(mapUser);
        Iterator iter = list.iterator();
        User user3 = null;
        while(iter.hasNext()){
            user3 = (User)iter.next();
            System.out.println(user3.getUsername()+"-->"+user3.getPassword());
        }


        if (umi.delUser(2L)){
            logger.info("删除ID为2的记录");
         }
    }
//    public void getUser(){
//        Configuration cfg = new Configuration();
//        cfg.configure();
//
//        @SuppressWarnings("deprecation")
//        SessionFactory sf = cfg.buildSessionFactory();
//
//        Session s = sf.openSession();
//
//        Transaction tx = s.beginTransaction();
//        User user = new User();
//        user.setNickname("777777");
//        user.setPassword("aaaaaaaa");
//        user.setUsername("voker");
//        user.setId(2);
//
//        //s.save(user);
//        s.saveOrUpdate(user);
//        tx.commit();
//        s.close();
//    }

//    public void testThreadLocal(){
//        HibernateUtil.initSessionFactory();
//        Session session = HibernateUtil.getSession();
//        HibernateUtil.transaction();
//        User u = new User();
//        u.setName("test");
//        session.save(u);
//        HibernateUtil.commitTransaction();
//        HibernateUtil.closeSession();
//    }
}
